package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.EssayCourseStudyRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

public interface EssayCourseStudyRecordRepository extends JpaRepository<EssayCourseStudyRecord, Integer> {
    //根据任务id查询学习记录
    @Query("select ecsr from EssayCourseStudyRecord ecsr where ecsr.taskId = ?1")
    EssayCourseStudyRecord findByTaskId(Integer taskId);

    //根据taskId和phone查询记录条数
    @Query(value = "select count(ecsr) from EssayCourseStudyRecord  ecsr where ecsr.taskId = ?1 and ecsr.phone = ?2")
    Integer findCountByTaskIdAndPhone(Integer taskId, String phone);

    //更新当前用户的章节结束时间
    @Query(value = "update EssayCourseStudyRecord ecsr set ecsr.endTime = ?1 where ecsr.phone = ?2 and ecsr.taskId = ?3")
    @Modifying
    @Transactional
    void updateEndTime(Date endTime, String phone, Integer taskId);

    @Transactional
    @Modifying
    @Query(value = "delete from EssayCourseStudyRecord ecsr where ecsr.taskId = ?1 and ecsr.phone = ?2")
    void deleteByTaskIdAndPhone(Integer taskId, String phone);
}